/*
 * $Id: ScrollableImpl.java 23 2009-03-31 23:37:36Z aaron.day $
 */
package org.crazydays.gameplan.swing;


import java.awt.Component;
import java.awt.Dimension;
import java.awt.Rectangle;

import javax.swing.JComponent;
import javax.swing.Scrollable;
import javax.swing.SwingConstants;

/**
 * ScrollableImpl
 */
public class ScrollableImpl
        implements Scrollable
{
	/** component */
	protected JComponent component;

	/** max unit increment */
	protected int maxUnitIncrement = 1;

	/**
	 * ScrollableImpl constructor.
	 * 
	 * @param component Component
	 */
	public ScrollableImpl(JComponent component)
	{
		setComponent(component);
	}

	/**
	 * Set component.
	 * 
	 * @param component Component
	 */
	public void setComponent(JComponent component)
	{
		this.component = component;
	}

	/**
	 * Get component.
	 * 
	 * @return Component
	 */
	public JComponent getComponent()
	{
		return component;
	}

	/**
	 * Set max unit increment.
	 * 
	 * @param maxUnitIncrement Max unit increment
	 */
	public void setMaxUnitIncrement(int maxUnitIncrement)
	{
		this.maxUnitIncrement = maxUnitIncrement;
	}

	/**
	 * Get max unit increment.
	 * 
	 * @return Max unit increment
	 */
	public int getMaxUnitIncrement()
	{
		return maxUnitIncrement;
	}

	/**
	 * Get preferred scrollable viewport size.
	 * 
	 * @return Dimension
	 * @see javax.swing.Scrollable#getPreferredScrollableViewportSize()
	 * @see Component#getPreferredSize()
	 */
	public Dimension getPreferredScrollableViewportSize()
	{
		return getComponent().getPreferredSize();
	}

	/**
	 * Get scrollable tracks viewport height.
	 * 
	 * @return Tracks
	 * @see javax.swing.Scrollable#getScrollableTracksViewportHeight()
	 */
	public boolean getScrollableTracksViewportHeight()
	{
		return false;
	}

	/**
	 * Get scrollable tracks viewport width.
	 * 
	 * @return Tracks
	 * @see javax.swing.Scrollable#getScrollableTracksViewportWidth()
	 */
	public boolean getScrollableTracksViewportWidth()
	{
		return false;
	}

	/**
	 * Get scrollable unit increment.
	 * 
	 * @param visibleRect Visible rectangle
	 * @param orientation Orientation
	 * @param direction Direction
	 * @return Increment
	 * @see javax.swing.Scrollable#getScrollableUnitIncrement(Rectangle,int,int)
	 */
	public int getScrollableUnitIncrement(Rectangle visibleRect,
	        int orientation, int direction)
	{
		// Get the current position.
		int currentPosition = 0;
		if (orientation == SwingConstants.HORIZONTAL) {
			currentPosition = visibleRect.x;
		} else {
			currentPosition = visibleRect.y;
		}

		// Return the number of pixels between currentPosition
		// and the nearest tick mark in the indicated direction.
		if (direction < 0) {
			int newPosition =
			        currentPosition
			                - (currentPosition / getMaxUnitIncrement())
			                * getMaxUnitIncrement();
			return (newPosition == 0) ? getMaxUnitIncrement()
			        : newPosition;
		} else {
			return ((currentPosition / getMaxUnitIncrement()) + 1)
			        * getMaxUnitIncrement() - currentPosition;
		}
	}

	/**
	 * Get scrollable block increment.
	 * 
	 * @param visibleRect Visisble rectangle
	 * @param orientation Orientation
	 * @param direction Direction
	 * @return Increment
	 * @see javax.swing.Scrollable#getScrollableBlockIncrement(Rectangle,int,int)
	 */
	public int getScrollableBlockIncrement(Rectangle visibleRect,
	        int orientation, int direction)
	{
		if (orientation == SwingConstants.HORIZONTAL) {
			return visibleRect.width - getMaxUnitIncrement();
		} else {
			return visibleRect.height - getMaxUnitIncrement();
		}
	}
}
